# Author：liyimei
# Time：2023/5/17 16:10
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class Base:
    '''
    基础类,封装了selenium基础方法,供page包下面的类继承
    '''

    def __init__(self, browser):
        # 打开浏览器,设置driver
        if browser == 'chrome':
            self.driver = webdriver.Chrome()
        elif browser == 'firefox':
            self.driver = webdriver.Firefox()
        elif browser == 'edge':
            self.driver = webdriver.Edge()
        else:
            print('不支持该浏览器')
            self.driver = None  # 不写下面可能会报黄(找不到)

    def get(self, url):
        # 请求目标网址
        try:
            self.driver.get(url)
        except Exception as e:
            print('输入的地址不正确')

    def find_element(self, locator):
        # locator = (By.xxx,value)
        # 定位元素
        try:
            ele = self.driver.find_element(*locator)
            print(ele.get_attribute('outerHTML'))
            return ele
        except Exception as e:
            print('元素定位失败')

    def click(self, locator):
        ele = self.find_element(locator)
        if ele is not None:
            ele.click()
        else:
            print('元素定位失败')


if __name__ == '__main__':
    base = Base('chrome')
    base.get('https://www.baidu.com')
    base.find_element((By.ID, 'kw'))
    base.click((By.ID, 'su'))
    time.sleep(3)
